package com.lujianfei.filesend.widget

import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.ObservableBoolean
import androidx.lifecycle.ViewModel
import com.lujianfei.common.KeyboardUtils
import com.lujianfei.filesend.base.BaseView
import com.lujianfei.filesend.base.toast
import com.lujianfei.filesend.databinding.WidgetInputLayoutBinding


/**
 * Author: lujianfei
 * Date: 2023/10/18 10:15
 * Description: 输入框组件
 */

class WidgetInputLayout(context: Context, attrs: AttributeSet? = null) : BaseView(context, attrs) {

    private var binding:WidgetInputLayoutBinding ?= null
    private val viewModel:WidgetInputViewModel by lazy { buildViewModel() }
    var onTextSend:((String)->Unit) ?= null
    var onPhotoClick:(()->Unit) ?= null
    init {
        binding = WidgetInputLayoutBinding.inflate(LayoutInflater.from(context), this, true)
        binding?.vm = viewModel
        binding?.onMoreClick = OnClickListener {
            viewModel.showMorePanel.set(!viewModel.showMorePanel.get())
        }
        binding?.onSendClick = OnClickListener {
            if (binding?.editText?.text?.isBlank() == true) {
                toast("请输入内容")
                return@OnClickListener
            }
            onTextSend?.invoke(binding?.editText?.text?.toString()?:"")
            binding?.editText?.setText("")
            KeyboardUtils.hideKeyboard(binding?.editText)
        }
        binding?.onPhotoClick = OnClickListener {
            onPhotoClick?.invoke()
        }
    }
}

class WidgetInputViewModel:ViewModel() {
    val showMorePanel = ObservableBoolean(false)
}